package com.gzbugu.polytechsasia.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gzbugu.polytechsasia.dao.DatabaseLinkDAO;
import com.gzbugu.polytechsasia.dao.IUserDAO;
import com.gzbugu.polytechsasia.dao.SQLExecuter;
import com.gzbugu.polytechsasia.vo.User;

/**
 * User dao
 * @author gauze
 *
 */
public class UserDAO implements IUserDAO {

	private static Logger log = LoggerFactory.getLogger(UserDAO.class);

	/* (non-Javadoc)
	 * @see com.gzbugu.polytechsasia.dao.impl.IUserDAO#save(com.gzbugu.polytechsasia.vo.User)
	 */
	public void save(User user) throws Exception {
		try {
			log.debug("save user " + user);
			String prepareSql = "INSERT INTO User (passport,password) VALUES(?,?)";
			List<Object> params = new ArrayList<Object>();
			params.add(user.getPassport());
			params.add(user.getPassword());
			SQLExecuter.update(prepareSql, params);
			log.debug("save user successful");
		} catch (Exception e) {
			log.error("save user failed", e);
			throw e;
		}
	}

	/* (non-Javadoc)
	 * @see com.gzbugu.polytechsasia.dao.impl.IUserDAO#findById(int)
	 */
	public User findById(int id) throws SQLException {
		log.debug("find user by id " + id);
		String prepareSql = "SELECT * FROM User WHERE id = ?";
		Connection connection = DatabaseLinkDAO.getConnection();
		try {
			PreparedStatement statement = connection
					.prepareStatement(prepareSql);
			statement.setInt(1, id);
			ResultSet resultSet = statement.executeQuery();
			if (resultSet.next()) {
				User user = new User();
				user.setId(resultSet.getInt(1));
				user.setPassport(resultSet.getString(2));
				user.setPassword(resultSet.getString(3));
				log.debug("find user successful");
				DatabaseLinkDAO.free(resultSet, statement, connection);
				return user;
			} else {
				log.debug("find user failed and return null");
				DatabaseLinkDAO.free(resultSet, statement, connection);
				return null;
			}
		} catch (SQLException e) {
			log.error("find user failed", e);
			DatabaseLinkDAO.free(null, null, connection);
			throw e;
		}
	}

	/* (non-Javadoc)
	 * @see com.gzbugu.polytechsasia.dao.impl.IUserDAO#findByPassport(java.lang.String)
	 */
	public User findByPassport(String passport) throws SQLException {
		log.debug("find user by passport " + passport);
		String prepareSql = "SELECT * FROM User WHERE passport = ?";
		Connection connection = DatabaseLinkDAO.getConnection();
		try {
			PreparedStatement statement = connection
					.prepareStatement(prepareSql);
			statement.setString(1, passport);
			ResultSet resultSet = statement.executeQuery();
			if (resultSet.next()) {
				User user = new User();
				user.setId(resultSet.getInt(1));
				user.setPassport(resultSet.getString(2));
				user.setPassword(resultSet.getString(3));
				log.debug("find user successful");
				DatabaseLinkDAO.free(resultSet, statement, connection);
				return user;
			} else {
				log.debug("find user failed and return null");
				DatabaseLinkDAO.free(resultSet, statement, connection);
				return null;
			}
		} catch (SQLException e) {
			log.error("find user failed", e);
			DatabaseLinkDAO.free(null, null, connection);
			throw e;
		}
	}

}
